package com.kla.power.scheduled;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kla.power.entity.DeviceEntity;
import com.kla.power.entity.InverterAmountStatisticsDayEntity;
import com.kla.power.entity.InverterElectricEntity;
import com.kla.power.service.IDeviceService;
import com.kla.power.service.IInverterAmountStatisticsDayService;
import com.kla.power.service.IInverterElectricService;
import com.kla.power.util.ConstUtil;
import com.kla.power.util.SnowflakeIdWorker;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 逆变器电量统计分析定时任务
 * 每日凌晨一点更新前一天发电量最大的数据
 */
@Component
@EnableScheduling
@Slf4j
public class InverterAmountStatisticsScheduled {

    @Resource
    private IInverterElectricService iInverterElectricService;
    @Resource
    private IDeviceService iDeviceService;
    @Resource
    private IInverterAmountStatisticsDayService iInverterAmountStatisticsDayService;

    @Scheduled (cron = "0 10 0 * * ?")
    public void statisticsDay () {
        log.info ( "每天0点10分统计每组逆变器前一天发电量..." );
        DeviceEntity deviceEntity = new DeviceEntity ();
        deviceEntity.setDeviceType ( ConstUtil.DEVICE_TYPE_INVERTER );
        List<DeviceEntity> deviceEntities = iDeviceService.list ( new LambdaQueryWrapper<DeviceEntity> ().eq ( DeviceEntity :: getDeviceType, 2 ) );
        for ( DeviceEntity entity : deviceEntities ) {
            // 查询所有设备最后一条数据均需备份
            InverterElectricEntity inverterElectricEntity = new InverterElectricEntity ();
            inverterElectricEntity.setSerialNumber ( entity.getDeviceSn () );
            inverterElectricEntity = iInverterElectricService.queryMaxData ( inverterElectricEntity );
            if ( inverterElectricEntity != null ) {
                InverterAmountStatisticsDayEntity inverterAmount = iInverterAmountStatisticsDayService.getBaseMapper ().selectOne ( new QueryWrapper<InverterAmountStatisticsDayEntity> ().eq ( "inverterSn", inverterElectricEntity.getSerialNumber () ).
                        eq ( "collectorTime", inverterElectricEntity.getCreateTime () ) );
                if ( inverterAmount == null ) { // 同一个逆变器同一天只能备份一次
                    InverterAmountStatisticsDayEntity inverterAmountStatisticsDayEntity = new InverterAmountStatisticsDayEntity ();
                    inverterAmountStatisticsDayEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
                    inverterAmountStatisticsDayEntity.setCollectorTime ( inverterElectricEntity.getCreateTime () );
                    LocalDate localMonth = inverterElectricEntity.getCreateTime ().toInstant ().atZone ( ZoneId.systemDefault () ).toLocalDate ();
                    int month = localMonth.getMonthValue (); // 直接获取月份值，从1开始
                    inverterAmountStatisticsDayEntity.setMonth ( String.valueOf ( month ) );
                    inverterAmountStatisticsDayEntity.setInverterSn ( inverterElectricEntity.getSerialNumber () );
                    inverterAmountStatisticsDayEntity.setYesterdayPower ( inverterElectricEntity.getTodayEnergy () );
                    LocalDate localDate = LocalDate.now ().minusDays ( 1 );//1代表提前多少天
                    String yearAndMonth = localDate.getYear () + "-" + ( localDate.getMonthValue () < 10 ? "0" + localDate.getMonthValue () : localDate.getMonthValue () );
                    inverterAmountStatisticsDayEntity.setMonthTime ( yearAndMonth );
                    iInverterAmountStatisticsDayService.saveSingle ( inverterAmountStatisticsDayEntity );
                }
            }
        }
    }

}
